﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PersonaLibrary.Data;
using PersonaLibrary.View;

namespace PersonaLibrary.Commands.EditCommands
{
	internal sealed class DepartmentDelete : CommandBase
	{
		public override bool CanExecute(object parameter)
		{
			return parameter is int;
		}

		public override void Execute(object parameter)
		{
			DeleteView deleteView = new DeleteView();
			if (deleteView.ShowDialog().HasValue && deleteView.DialogResult.Value)
			{
				using (LinqToDbDataContext context = new LinqToDbDataContext())
				{
					context.ConnectionJobEmplDeps.DeleteAllOnSubmit(context.ConnectionJobEmplDeps.Where
						(
							x => x.DepartmentId == (int)parameter
						));

					context.Departments.DeleteOnSubmit(context.Departments.FirstOrDefault(x => ((Department)x).Id == (int)parameter));

					context.SubmitChanges();
				}
			}
		}
	}
}
